package com.ifast.data.controller;


import java.util.Arrays;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ifast.common.base.AdminBaseController;
import com.ifast.data.domain.DataPageinfoDO;
import com.ifast.data.domain.DataQueryDO;
import com.ifast.data.service.DataQueryService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 查询页面配置
 * </pre>
 * <small> 2018-04-18 11:00:40 | lvyq</small>
 */
@Controller
@RequestMapping("/data/dataQuery")
public class DataQueryController extends AdminBaseController {
	@Autowired
	private DataQueryService dataQueryService;
	
	@GetMapping()
	@RequiresPermissions("data:dataQuery:dataQuery")
	String DataQuery(){
	    return "data/dataQuery/dataQuery";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("data:dataQuery:dataQuery")
	public Result<Page<DataQueryDO>> list(DataQueryDO dataQueryDTO){
        Wrapper<DataQueryDO> wrapper = new EntityWrapper<DataQueryDO>(dataQueryDTO);
        Page<DataQueryDO> page = dataQueryService.selectPage(getPage(DataQueryDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("data:dataQuery:add")
	String add(){
	    return "data/dataQuery/add";
	}

	@GetMapping("/edit/{qid}")
	@RequiresPermissions("data:dataQuery:edit")
	String edit(@PathVariable("qid") Long qid,Model model){
		DataQueryDO dataQuery = dataQueryService.selectById(qid);
		model.addAttribute("dataQuery", dataQuery);
	    return "data/dataQuery/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("data:dataQuery:add")
	public Result<String> save( DataQueryDO dataQuery){
		dataQueryService.insert(dataQuery);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("data:dataQuery:edit")
	public Result<String>  update( DataQueryDO dataQuery){
		dataQueryService.updateById(dataQuery);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("data:dataQuery:remove")
	public Result<String>  remove( Long qid){
		dataQueryService.deleteById(qid);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("data:dataQuery:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] qids){
		dataQueryService.deleteBatchIds(Arrays.asList(qids));
		return Result.ok();
	}
	
	
	@GetMapping("/api/v1/getDataQuery")
	@ResponseBody
	public Result<Map<String,Object>> getPageinfo(DataQueryDO dataQuery) {
		Wrapper<DataQueryDO> wrapper = new EntityWrapper<DataQueryDO>(dataQuery);
		Map<String,Object> dataQueryMap = dataQueryService.selectMap(wrapper);
		if(dataQueryMap!=null&&dataQueryMap.size()>0) {
			return Result.ok(dataQueryMap);
		}
		return Result.ok();
	}
	
	
	@GetMapping("/api/v1/getDataQuery2")
	@ResponseBody
	public JSONPObject getPageinfo(String jsonpCallback,DataQueryDO dataQuery) {
		Wrapper<DataQueryDO> wrapper = new EntityWrapper<DataQueryDO>(dataQuery);
		Map<String,Object> dataQueryMap = dataQueryService.selectMap(wrapper);
		if(dataQueryMap!=null&&dataQueryMap.size()>0) {
			return new JSONPObject(jsonpCallback, Result.ok(dataQueryMap));
		}
		return new JSONPObject(jsonpCallback, Result.ok());
	}
	
}
